package com.java.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.druid.support.logging.Resources;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.StringUtil;

@RestController
public class UploadController {
	String comp=StringUtil.GetProject();

	@Autowired
	private HttpServletRequest request;
	//上传文件
	@CrossOrigin(origins = "*")
	@RequestMapping("/upload")
	@ResponseBody
    public JsonBody uploadFile(@RequestBody MultipartFile files) throws IOException {

        try {
        	//E:\my_task\202206\wx19611600068\springboot\src\main\resources\META-INF\resources\file
			ApplicationHome applicationHome = new ApplicationHome(this.getClass());
			String absolutePath = applicationHome.getDir().getParentFile()
					.getParentFile().getAbsolutePath()+"\\src\\main\\resources\\META-INF\\resources\\file\\";
			File path = new File(absolutePath);

			if (!path.exists()) {
				path.mkdirs();//文件夹不存在，创建文件夹
			}
			String originalFilename = files.getOriginalFilename();//获得原始的文件名 "a.jpg"
			int index = originalFilename.lastIndexOf(".");
			String newName = originalFilename.substring(0, index) + "_" + UUID.randomUUID().toString() + originalFilename.substring(index);
			files.transferTo(new File(absolutePath + newName));//不出现异常，就是文件上传成功
			JsonBody jb=Util.SetMap("上传成功",true);
			jb.setData("/file/" + newName);
			return jb;
		} catch (Exception e) {
				return Util.SetMap("上传失败，请检查上传接口配置",false);
		}

    }

    private void copyFileUsingFileStreams(File source,File datedirs,String newfilename)
            throws IOException {
    	comp+=comp.toLowerCase().indexOf("webroot")<0?"/WebRoot/file/":"";//同时复制附件到项目下面
    	//新文件
        File newFile = new File(comp+File.separator+datedirs+File.separator+newfilename);
        //判断目标文件所在的目录是否存在
        if(!newFile.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        InputStream input = null;
        OutputStream output = null;
        try {
               input = new FileInputStream(source);
               output = new FileOutputStream(newFile);
               byte[] buf = new byte[1024];
               int bytesRead;
               while ((bytesRead = input.read(buf)) > 0) {
                   output.write(buf, 0, bytesRead);
               }
        } finally {
            input.close();
            output.close();
        }
    }


}


